﻿/*  mfe-proj - Mystery Fiction Encyclopedia

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.*/

/*  Written by leyang <mysterybbs@gmail.com>  */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mfe.bll;
using System.Threading;
using System.Globalization;
using System.Resources;

namespace mfe_proj.Forms
{
    public partial class MainForm : Form
    {
        Forms.NavigationForm navForm;
        Forms.ContentViewer conForm;
        Control focused = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            MinimumSize = new Size(760, 450);

            splitContainer1.Panel2.BackColor = Color.White;

            //load the nav
            navForm = new NavigationForm();
            navForm.DoubleClick += new NavigationFormEventHandler(navForm_DoubleClick);
            navForm.NavigationLoaded += new NavigationFormEventHandler(navForm_NavigationLoaded);
            navForm.LoadData(0);
            navForm.DockablePanel.Parent = splitContainer1.Panel1;

            navForm.Focus();

            //load start page
            if (conForm == null)
            {
                conForm = new Forms.ContentViewer();
                conForm.LoadStartPage();
                conForm.DockablePanel.Parent = splitContainer1.Panel2;
            }
        }

        void navForm_NavigationLoaded(object sender, NavigationFormEventArgs e)
        {
            lblTotalCount.Text = string.Concat(e.Count, " ", Program.Resource.GetString("strItem"));
        }

        void navForm_DoubleClick(object sender, NavigationFormEventArgs e)
        {
            if (conForm == null)
            {
                conForm = new Forms.ContentViewer();
                conForm.DockablePanel.Parent = splitContainer1.Panel2;
            }
            conForm.ItemGuidID = navForm.SelectedGuidID; //used for other purpose such as Refresh()
            conForm.LoadItem(navForm.SelectedGuidID);

            LoadRelatedItems();
        }

        private void LoadRelatedItems()
        {
            //load related items
            List<Item> relatedItems = Item.RelatedItemCollection(conForm.ItemGuidID);
            btnRelatedItems.DropDownItems.Clear();
            if (relatedItems.Count == 0)
            {
                btnRelatedItems.Text = Program.Resource.GetString("strRelatedItem");
                btnRelatedItems.Enabled = false;
                return;
            }
            btnRelatedItems.Enabled = true;
            btnRelatedItems.Text = string.Concat(Program.Resource.GetString("strRelatedItem"), "(", relatedItems.Count, ")");
            foreach (Item item in relatedItems)
            {
                ToolStripMenuItem mnu = new ToolStripMenuItem(item.Title);
                mnu.Tag = item.ItemGuidID;
                mnu.Click += new EventHandler(btnRelatedItemsMenu_Click);
                btnRelatedItems.DropDownItems.Add(mnu);
            }
        }

        void btnRelatedItemsMenu_Click(object sender, EventArgs e)
        {
            conForm.ItemGuidID = ((ToolStripMenuItem)sender).Tag.ToString();
            conForm.LoadItem(conForm.ItemGuidID);
            LoadRelatedItems();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Program.Resource.GetString("strDeleteConfirmationMsg"), Program.Resource.GetString("strDeleteConfirmation"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (Item.DeleteCascade(navForm.SelectedGuidID))
                    navForm.RemoveFromTree();
            }
        }

        private void btnNewItem_Click(object sender, EventArgs e)
        {
            Forms.ItemForm itemForm = new Forms.ItemForm();
            itemForm.ItemChanged += new ItemFormEventHandler(itemForm_ItemChanged);
            itemForm.ShowDialog();
        }

        private void btnEditItem_Click(object sender, EventArgs e)
        {
            if (navForm.SelectedGuidID == null || navForm.SelectedGuidID == string.Empty) return;
            Forms.ItemForm itemForm = new Forms.ItemForm();
            itemForm.ItemChanged += new ItemFormEventHandler(itemForm_ItemChanged);
            itemForm.IsNew = false;
            itemForm.ItemGuidID = navForm.SelectedGuidID;
            itemForm.ShowDialog();
        }

        void itemForm_ItemChanged(object sender, ItemFormEventArgs e)
        {
            //reload the navForm for now, in the future should just update what's needed
            Program.ItemCollection.Clear();
            Program.ItemCollection = null;
            navForm.LoadData(0);
        }

        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            ///TODO: turn off header and footer when printing. this can only be done in register?
            ///downside is must have write permission for register key.
            ///refer to http://support.microsoft.com/kb/313723
            
            ///TODO: printing the background color, again, can be done only in register?
            ///refer to http://stackoverflow.com/questions/48278/how-to-print-css-applied-background-images-with-the-winforms-webbrowser-control
            ///
            if (conForm == null) return;
            conForm.PrintPreview();
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            AboutForm aboutForm = new AboutForm();
            aboutForm.ShowDialog();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            //refresh what's displayed in the content window
            if (conForm == null) return;
            conForm.LoadItem(conForm.ItemGuidID);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void splitContainer1_MouseDown(object sender, MouseEventArgs e)
        {
            focused = WinUtility.GetFocused(this.Controls);
        }

        private void splitContainer1_MouseUp(object sender, MouseEventArgs e)
        {
            if (focused != null)
            {
                // Return focus and clear the temp variable for 
                // garbage collection
                focused.Focus();
                focused = null;
            }
        }

        private void btnChineseSimplified_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Language = "zh-CHS";
            Properties.Settings.Default.Save();
            Controls.Clear();
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CHS");
            InitializeComponent();
            conForm = null;
            MainForm_Load(sender, e);
        }

        private void btnEnglish_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Language = "en-US";
            Properties.Settings.Default.Save();
            Controls.Clear();
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            InitializeComponent();
            conForm = null;
            MainForm_Load(sender, e);
        }
    }
}
